<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取类型" prop="extractType">
              <el-select v-model="form.extractType" placeholder="请选择抽取类型" style="width:100%;">
                <el-option
                  v-for="dict in extractTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取地点" prop="extractAddress">
              <el-select v-model="form.extractAddress" placeholder="请选择抽取地点" style="width:100%;">
                <el-option
                  v-for="dict in extractAddressOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取人" prop="extractUser">
              <el-input v-model="form.extractUser" placeholder="请输入抽取人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取人单位" prop="extractUnit">
                <el-input v-model="form.extractUnit" placeholder="请输入抽取人单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="抽取项目" prop="extractItem">
          <el-input v-model="form.extractItem" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单位所属库" prop="randomUnittypeId">
          <el-select v-model="form.randomUnittypeId" placeholder="请选择单位库" clearable size="small" style="width:100%;" @change="UnitTypeChange">
            <el-option
              v-for="type in unitTypeOptions"
              :key="type.unittypeId"
              :label="type.unittypeName"
              :value="type.unittypeId"
            />
          </el-select>
        </el-form-item>
        <!-- 库下对应单位表格 -->
        <el-table v-loading="loading" id="unitTable" :data="unitList">
          <el-table-column label="单位序号" align="center" prop="unitSort" />
          <el-table-column label="单位名称" align="center" prop="unitName" />
          <el-table-column label="资质等级" align="center" prop="unitNaturalGrade" />
          <el-table-column label="资质范围" align="center" prop="unitNaturalScope" />
          <el-table-column label="注册地址" align="center" prop="unitAddress" />
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer" style="float:right;margin-top:50px;">
        <el-button type="primary" @click="handleAdd">抽 取</el-button>
        <el-button @click="cancel">重 置</el-button>
      </div>

      <!-- 抽签过程展示窗口 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <img src="../../../assets/images/random.gif" class="user-avatar" style="height:200px;margin-left:25%;">
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">停 止</el-button>
          <el-button @click="gifcancel">取 消</el-button>
        </div>
      </el-dialog>

      <!-- 抽签结果展示窗口 -->
      <el-dialog :title="title" :visible.sync="resultOpen" width="1000px" append-to-body>
      <el-form ref="resultform" :model="resultform" size="medium" label-width="100px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取类型" prop="extractType">
              <el-select v-model="resultform.extractType" placeholder="请选择抽取类型" style="width:100%;" >
                <el-option
                  v-for="dict in extractTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取地点" prop="extractAddress">
              <el-select v-model="resultform.extractAddress" placeholder="请选择抽取地点" style="width:100%;">
                <el-option
                  v-for="dict in extractAddressOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="抽取人" prop="extractUser">
              <el-input v-model="resultform.extractUser" placeholder="请输入抽取人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="抽取人单位" prop="extractUnit">
                <el-input v-model="resultform.extractUnit" placeholder="请输入抽取人单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="抽取项目" prop="extractItem">
          <el-input v-model="resultform.extractItem" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单位所属库" prop="randomUnittypeId">
          <el-select v-model="resultform.randomUnittypeId" placeholder="请选择单位库" clearable size="small" style="width:100%;" @change="UnitTypeChange">
            <el-option
              v-for="type in unitTypeOptions"
              :key="type.unittypeId"
              :label="type.unittypeName"
              :value="type.unittypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="中签单位名称" prop="randomUnitName">
          <el-input v-model="resultform.randomUnitName" placeholder="请输入中签单位名称" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listRandom, getRandom, delRandom, addRandom, updateRandom, exportRandom } from "@/api/unit/random";
import { listUnit,randomlist, getUnit, delUnit, addUnit, updateUnit, exportUnit } from "@/api/unit/unit";
import { listUnittype, getUnittype } from "@/api/unit/unittype";

export default {
  name: "Random",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位抽签表格数据
      randomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //结果返回显示层
      resultOpen:false,
      // 抽取类型字典
      extractTypeOptions: [],
      // 抽取地点字典
      extractAddressOptions: [],
      // 单位类型字典
      unitTypeOptions: [],
      // 单位信息表格数据
      unitList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        randomUnitId: null,
        extractType: null,
        randomUnittypeId: null,
        extractItem: null,
        randomUnittypeName: null,
        randomUnitName: null,
        extractUser: null,
        extractUnit: null,
        extractAddress: null,
      },
      UnitqueryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: null,
        unitNaturalGrade: null,
        unitNaturalScope: null,
        unitType: null,
      },
      unitTypeParams:{
        pageNum: 1,
        pageSize: 10,
        unittypeName: null,
      },
      // 表单参数
      form: {},
      // 结果表单参数
      resultform: {},
      // 表单校验
      rules: {
        extractType: [
          { required: true, message: "抽取类别不能为空", trigger: "blur" }
        ],
        extractAddress: [
          { required: true, message: "抽取地点不能为空", trigger: "blur" }
        ],
        extractUser: [
          { required: true, message: "抽取人不能为空", trigger: "blur" }
        ],
        extractUnit: [
          { required: true, message: "抽取单位不能为空", trigger: "blur" }
        ],
        extractItem: [
          { required: true, message: "抽取项目不能为空", trigger: "blur" }
        ],
        randomUnittypeId: [
          { required: true, message: "抽取库不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listUnittype(this.unitTypeParams).then(response => {
        this.unitTypeOptions = response.rows;
      });
    this.getDicts("extract_type").then(response => {
      this.extractTypeOptions = response.data;
    });
    this.getDicts("extract_address").then(response => {
      this.extractAddressOptions = response.data;
    });
  },
  methods: {
    /** 查询单位抽签列表 */
    getList() {
      listRandom(this.queryParams).then(response => {
        this.randomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 抽取类型字典翻译
    extractTypeFormat(row, column) {
      return this.selectDictLabel(this.extractTypeOptions, row.extractType);
    },
    // 抽取地点字典翻译
    extractAddressFormat(row, column) {
      return this.selectDictLabel(this.extractAddressOptions, row.extractAddress);
    },
    // 单位库类型翻译
    unitTypeFormat(row, column) {
      return this.selecttypeLabel(this.unitTypeOptions, row.randomUnittypeId);
    },
    //转换单位库类型
    selecttypeLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].unittypeId == ('' + value)) {
          actions.push(datas[key].unittypeName);
          return true;
        }
      })
      return actions.join('');
    },
    //单位选择select改变方法
    UnitTypeChange(value){
      //让table显现出来

      //查询table数据
      this.UnitqueryParams.unitType = value;
      randomlist(this.UnitqueryParams).then(response => {
        this.unitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    //抽取动画取消
    gifcancel() {
      this.open = false;
    },
    // 表单重置
    reset() {
      this.form = {
        randomId: null,
        randomUnitId: null,
        extractType: null,
        randomUnittypeId: null,
        extractItem: null,
        randomUnittypeName: null,
        randomUnitName: null,
        extractUser: null,
        createBy: null,
        extractUnit: null,
        createTime: null,
        randomBan: "0",
        extractAddress: null,
        updateBy: null,
        randomBanRansom: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.randomId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 抽取按钮操作 */
    handleAdd() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = true;
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const randomId = row.randomId || this.ids
      getRandom(randomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单位抽签";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.open = false;
          if (this.form.randomId != null) {
            updateRandom(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRandom(this.form).then(response => {
              this.reset();
              this.msgSuccess("抽取成功");
              this.resultform = response.data;
              this.resultOpen = true;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const randomIds = row.randomId || this.ids;
      this.$confirm('是否确认删除单位抽签编号为"' + randomIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRandom(randomIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有单位抽签数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRandom(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
